/**
 * 班级服务
 */
angular
.module('myApp')
.factory('classManager', function($http, $q) {
  return {
    /**
     * 获取班型、班次信息
     */
    getClasses: function(query) {
      var def = $q.defer();

      $http.get('/lesson/class', {params: query})
        .then(function(response) {
          (response.data.code === 200)
            ? def.resolve(response.data.data)
            : def.reject(response.data.data);
        }, function(response) {
          def.reject({msg: response.status+': '+response.statusText});
        });

      return def.promise;
    },
    /**
     * 根据班型 id 获取班型、班次信息
     */
    getClass: function(id) {
      var def = $q.defer();

      $http.get('/lesson/class/' + id)
        .then(function(response) {
          (response.data.code === 200)
            ? def.resolve(response.data.data)
            : def.reject(response.data.data);
        }, function(response) {
          def.reject({msg: response.status+': '+response.statusText});
        });

      return def.promise;
    },
    /**
     * 获取课件信息
     */
    getCourse: function(id, type) {
      var def = $q.defer();

      var query = {
        course_id: id,
        type: type
      }

      $http.get('/lesson/course', {params: query})
        .then(function(response) {
          if (response.data.code === 200) {
            angular.isDefined(response.data.data.course)
              ? def.resolve(response.data.data.course.course_data)
              : def.reject(response.data.data);
          } else {
            def.reject(response.data.data);
          }
        }, function(response) {
          def.reject({msg: response.status+': '+response.statusText});
        });

      return def.promise;
    }
  }
});
